package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.mapper.PermissionMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IPermissionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
public class PermissionServiceImpl implements IPermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public void delete(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(Permission permission) {
        permissionMapper.insert(permission);
    }

    @Override
    public Permission get(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> listAll() {
        return permissionMapper.selectAll();
    }

    @Override
    public void update(Permission permission) {
        permissionMapper.updateByPrimaryKey(permission);
    }

    //使用插件翻页操作
    public PageInfo list(QueryObject queryObject) {
        int currentPage = queryObject.getCurrentPage();
        int pageSize = queryObject.getPageSize();
        PageHelper.startPage(currentPage, pageSize);
        List<Permission> list = permissionMapper.list(queryObject);
        return new PageInfo(list);
    }

    @Autowired
    private ApplicationContext ctx;//获取数据库中所有的权限表达式

    @Override
    public void reload() {
        //获取数据库所有的expression
        List<String> list = permissionMapper.selectForExpression();
        //通过spring上下文获取带有Controller注解的bean ,获取所有的controller
        Map<String, Object> beansWithAnnotation = ctx.getBeansWithAnnotation(Controller.class);
        //获得每一个map的value
        Collection<Object> controllers = beansWithAnnotation.values();
        for (Object controller : controllers) {
            //获取每个controller的方法
            Class<?> clz = controller.getClass().getSuperclass();
            Method[] methods = clz.getDeclaredMethods();
            for (Method method : methods) {
                //判断该方法是否有贴自定义的权限注解  因为这个方法是所有的controller方法  并不是所有的都贴了@RequierdPermission
                RequiresPermissions annotation = method.getAnnotation(RequiresPermissions.class);
                //如果有贴就需要处理
                if (annotation != null) {
                    Permission permission = new Permission();
                    //获得每个方法的注解的名字  部门编辑  删除  等...
                    //注解中指定的权限名称
                    String name1 = annotation.value()[1];
                    permission.setName(name1);
                    //使用工具类来做  参数类型是一样的
                    //注解中指定的权限表达式
                    String expression = annotation.value()[0];
                    //判断该表达式是否已经存在数据库
                    //如果不存在,就需要插入到数据库
                    if (!list.contains(expression)) {
                        permission.setExpression(expression);
                        permissionMapper.insert(permission);
                    }
                }
            }
        }
    }

    @Override
    public List<String> getPermissionsByEmpId(Long id) {
        return permissionMapper.getPermissionsByEmpId(id);
    }
}
